> A regular expression specifies a set of strings that matches it. This cheat sheet is based off Python 3's Regular Expressions (http://docs.python.org/3/library/re.html) but is designed for searches within Sublime Text. > Special Characters \ Escapes special characters or signals a special sequence. . Matches any single character except a newline. ^ Matches the start of the string. $ Matches the end of the string. * Greedily matches 0 or more repetitions of the preceding RE. *? Matches 0 or more repetitions of the preceding RE. + Greedily matches 1 or more repetitions of the preceding RE. +? Matches 1 or more repetitions of the preceding RE. ? Greedily matches 0 or 1 repetitions of the preceding RE. ?? Matches 0 or 1 repetitions of the preceding RE. A|B Matches A, if A is unmatched then matches B, where A and B are arbitrary REs. {m} Matches exactly m many repetitions of the previous RE. {m,n} Greedily matches from m many to n many repetitions of the previous RE. {m,n}? Matches m many to n many repetitions of the previous RE. [...] Indicates a set of characters to match. [amk] Matches 'a', 'm', or 'k'. [a-z] Matches 'a' through 'z'. [a-f0-7] Matches 'a' through 'f' or '0' through '7'. [a\-z] Matches 'a', '-', or 'z'. [a-] Matches 'a' or '-'. [-a] Matches 'a' or '-'. [(+*)] Matches '(', '+', '*', or ')'. [] matches special characters literally. [\w] Matches the character class for '\w'. See character classes. [^5] Matches anything other than '5'. '^' forms the complementary set only as the first character in a set. []()] Matches ']', '(', and ')'. ']' is taken literally only as the first character in a set. [()\]] Matches ']', '(', and ')'. (...) Matches the RE inside the parenthesis and assigns a new group. (?P<name>...) The RE matched is accessible by the group indicated by name. (?...) Extension notation which changes a RE's behavior. These do not assign a new group. (?aiLmsux) Sets the corresponding flag to each letter. Does not work within Sublime Text. (?:...) A non-capturing version of parenthesis. The matched substring cannot be retrieved later. (?P=name) Matches the substring matched by the group named name. (?#...) A comment, the contents are ignored. (?=...) Lookahead assertion, the preceding RE only matches if this matches. (?!...) Negative lookahead assertion, the preceding RE only matches if this doesn't match. (?<=...) Positive lookbehind assertion, the following RE will only match if preceded with this fixed length RE. (?<!...) Negative lookbehind assertion, the following RE will only match if not preceded with this fixed length RE. (?(id)true|false) If group id exists then uses the true RE, else use the false RE. > Character classes \1 Matches the contents of the group labelled by the same number. Acceptable numbers are 1-99. \A Matches at the start of the current string. \b Matches the empty string at the beginning or end of a word. \b matches the boundary between \w and \W. \B Matches the empty string not at the beginning or end of a word. \d Matches any Unicode decimal digit, including 0-9. \D Matches any Unicode non-decimal digit. \s Matches any Unicode whitespace character, including ' ', \t, \n, \r, \f and \v. \S Matches any Unicode non-whitespace character. \w Matches any Unicode word character, including a-z, A-Z, and 0-9. \W Matches any Unicode non-word character. \Z Matches at the end of the string. \a Matches the ASCII Bell (). \f Matches the ASCII Formfeed (). \n Matches the ASCII Linefeed. \r Matches the ASCII Carriage Return (). \t Matches the ASCII Horizontal Tab. \v Matches the ASCII Vertical Tab ().
Hello my name is bob
And this search term:
Find what: my name is (\w)+
Replace with: my name used to be $(1)
The search term works just fine but I can't figure out a way to actually do a replace using the regexp group.
Usually a back-reference is either $1
or \1
(backslash one) for the first capture group (the first match of a pattern in parentheses). So maybe try:
my name used to be \1
or
my name used to be $1
UPDATE: As several people have pointed out, your original capture pattern is incorrect and will only capture the final letter of the name rather than the whole name. You should use the following pattern to capture all of the letters of the name:
my name is (\w+)
Find part:
my name is (\w)+
With replace part:
my name used to be \1
Would return:
Hello, my name used to be b
Change find part to:
my name is (\w+)
And replace will be what you expect:
Hello, my name used to be bob
While (\w)+ will match "bob", it is not the grouping you want for replacement.
Use the ( ) parentheses in your search string
There is an important thing to emphasize! All the matched segments in your search string that you want to use in your replacement string must be embraced by ( ) parentheses, otherwise these matched segments won't be reachable with variables such as $1, $2,...nor \1, \2,.. and etc.
EXAMPLE:
We want to replace 'em' with 'px' but preserve the number values:
margin: 10em
margin: 2em
So we use the margin: $1px
as the replacement string.
CORRECT: Embrace the desired $1
matched segment by ( )
parentheses as following:
FIND: margin: ([0-9]*)em
(With parentheses)
REPLACE TO: margin: $1px
RESULT:
margin: 10px
margin: 2px
WRONG: The following regex pattern will match the desired lines but matched segments will not be available in replaced string as variables such as $1
:
FIND: margin: [0-9]*em
(Without parentheses)
REPLACE TO: margin: $1px
RESULT: ($1
is undefined)
margin: px
margin: px
Note that if you use more than 9 capture groups you have to use the syntax ${10}
.
$10
or \10
or \{10}
will not work.